#!/bin/bash

if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ]; then
        echo 'ERROR! addModule必要参数不能为空！参数要求: schoolCode, modules, version'
        exit -1;
fi

schoolCode=$1
modules=$2
version=$3
nasPath=/var/nas
# mysql域名
mysqlHost=`echo $schoolCode | tr '[A-Z]' '[a-z]'`
if [ -z "$mysqlHost" ]; then
        echo "ERROR! mysqlHost IS NULL."
        exit -1;
fi
mysqlHost=$mysqlHost"-mysql-m"

BX_HOME=$nasPath/$schoolCode
if [ ! -d "$BX_HOME" ]; then
        echo "ERROR! addModule $schoolCode not exists."
        exit -1;
fi

moduleList=${modules//,/ }
for module in $moduleList; do

    #基础模块 跳过复制配置文件和f_data
    if [ $module == 'bxn-score' ] || [ $module == 'bxn-office' ] || [ $module == 'bxn-timetable' ] || [ $module == 'microsite' ]; then
        echo "skip config $schoolCode : $module."
        continue;
    fi

    if [ ! -d "$nasPath/extends/$module" ]; then
            echo "ERROR! addModule $module file not exists."
            exit 0;
    fi

    if [ ! -d "$nasPath/extends/$module/$version/f_data/$module" ]; then
           mkdir -p $nasPath/extends/$module/$version/f_data/$module
    fi
done

for module in $moduleList; do
    #基础模块 跳过复制配置文件和f_data
    if [ $module == 'bxn-score' ] || [ $module == 'bxn-office' ] || [ $module == 'bxn-timetable' ] || [ $module == 'microsite' ]; then
        echo "skip config $schoolCode : $module."
        continue;
    fi
    
    cp $nasPath/extends/$module/$version/config/$module.properties $BX_HOME/config/
    cp -r $nasPath/extends/$module/$version/f_data/* $BX_HOME/f_data/


    sed -i "s/{ibc.schoolCode}/$schoolCode/g" $BX_HOME/config/$module.properties
    sed -i "s/127.0.0.1:3306/$mysqlHost:3306/g" $BX_HOME/config/$module.properties
done
echo "SUCCESS! end config $schoolCode : $module."